<?php
namespace App\Http\Controllers\Admin;


use Illuminate\Http\Response;
use App\Models\Admin\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;



class UserController extends Controller
{
    /**
     * @var UserRepository
     */
    private $User;


    /**
     * ApageController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->User= new Users();
    }

    /**
     * Display a listing of the resource.
     *用户列表
     * @return Response
     */
    public function index()
    {
        $users = $this->User->orderBy('id', 'desc')->paginate(15, ['*'], 'pageNum');

        return view('admin.User.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $roles = $this->role->all();

        return view('user::admin.users.create', compact('roles'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  CreateUserRequest $request
     * @return Response
     */
    public function store(CreateUserRequest $request)
    {
        $data = $this->mergeRequestWithPermissions($request);

        $this->user->createWithRoles($data, $request->roles, true);

        return redirect()->route('admin.user.user.index')
            ->withSuccess(trans('user::messages.user created'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int      $id
     * @return Response
     */
    public function edit(Request $request,$id)
    {
        if (!$user = $this->User->find($id)) {
            return redirect()->route('admin.user.user.index')
                ->withError(trans('user::messages.user not found'));
        }
        $users = $this->User->find($id);
        if($request->getMethod()=="POST"){
            $users->address = $request->input('address');
            $users->update();

            return redirect()->route('admin.User.index')->with('success','修改成功!');

        }
        return view('admin.User.edit', compact('users'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int               $id
     * @param  UpdateUserRequest $request
     * @return Response
     */
    public function update($id, UpdateUserRequest $request)
    {
        $data = $this->mergeRequestWithPermissions($request);

        $this->user->updateAndSyncRoles($id, $data, $request->roles);

        if ($request->get('button') === 'index') {
            return redirect()->route('admin.user.user.index')
                ->withSuccess(trans('user::messages.user updated'));
        }

        return redirect()->back()
            ->withSuccess(trans('user::messages.user updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int      $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->user->delete($id);

        return redirect()->route('admin.user.user.index')
            ->withSuccess(trans('user::messages.user deleted'));
    }

    public function sendResetPassword($user, Authentication $auth)
    {
        $user = $this->user->find($user);
        $code = $auth->createReminderCode($user);

        event(new UserHasBegunResetProcess($user, $code));

        return redirect()->route('admin.user.user.edit', $user->id)
            ->withSuccess(trans('user::auth.reset password email was sent'));
    }
}
